package sz.szsmk.citizencard.ui.newversion.fragment.presenter

import com.google.gson.Gson
import com.youzan.spiderman.utils.MD5Utils
import sz.szsmk.citizencard.DaggerApplication
import sz.szsmk.citizencard.log.L
import sz.szsmk.citizencard.model.network.bean.request.IBaseRequest
import sz.szsmk.citizencard.model.network.bean.request.ServiceNetReq
import sz.szsmk.citizencard.model.network.bean.response.BaseBean
import sz.szsmk.citizencard.model.network.bean.response.PrepayResponse
import sz.szsmk.citizencard.model.network.clarkbean.response.AreaInfoResponse
import sz.szsmk.citizencard.mvp.contract.IAppLifeContact.IAppLifeView
import sz.szsmk.citizencard.mvp.dzsmkpresenter.AppBasePresenter
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult
import sz.szsmk.citizencard.retrofitrxjava.newversion.ApiCallback
import sz.szsmk.citizencard.retrofitrxjava.newversion.HttpDateSource
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource
import sz.szsmk.citizencard.ui.newversion.fragment.view.LifeMvpView
import sz.szsmk.citizencard.util.ACache
import sz.szsmk.citizencard.util.AndroidUtils
import sz.szsmk.citizencard.util.Constant
import javax.inject.Inject

/**
 *  author : hyt
 *  date : 2020/11/12
 *  description :
 */
class LifePresenter@Inject constructor(var httpDateSourceNotUse: NetworkDateSource):
        AppBasePresenter<LifeMvpView>()  {

    //获取首页 模块数据
    fun queryAreaInfo(){
        val baseRequest = IBaseRequest()
        val access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token")
        baseRequest.access_token = access_token
        val androidId = AndroidUtils.getAndroidId(DaggerApplication.getIns())
        baseRequest.device_id = AndroidUtils.getAndroidId(DaggerApplication.getIns())
        val refresh_token = ACache.get(DaggerApplication.getIns()).getAsString("refresh_token")
        val key = "access_token=$access_token&device_id=$androidId&key=$refresh_token"
        val stringMd5 = MD5Utils.getStringMd5(key).toUpperCase()
        baseRequest.sign = stringMd5

        addSubscription(HttpDateSource.createApi()?.getAreaInfo(baseRequest), object : ApiCallback<AreaInfoResponse?> {
            override fun onSuccess(response: AreaInfoResponse?) {
                response?.let {
                    mvpView.getAreaInfoSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })
    }

    fun getWebsiteList(service_type: String?, site_type: String?, distance_type: String?, type: Int, mLatitude: String?, mLongitude: String?, pager: Int) {
        val serviceNetReq = ServiceNetReq()
        serviceNetReq.latitude = mLatitude
        serviceNetReq.longitude = mLongitude
        serviceNetReq.distance_type = distance_type
        serviceNetReq.page_num = pager.toString()
        serviceNetReq.page_size = "15"
        serviceNetReq.service_type = service_type
        serviceNetReq.site_type = site_type
        val json = Gson().toJson(serviceNetReq)
        L.e(Constant.TAG, "getWebsiteList----$json")

        addSubscription(HttpDateSource.createApi()?.websiteList(serviceNetReq), object : ApiCallback<PrepayResponse?> {
            override fun onSuccess(response: PrepayResponse?) {
                response?.let {
                    mvpView.getWebsiteListSuccess(it)
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })

    }

}